const mysql = require('mysql');
const pool = mysql.createPool({
        host: '127.0.0.1',
        user: 'root',
        password: 'root',
        database: 'yesmywine',
        connectionLimit: 20,
        timeout: 1000 * 20
    })
    //封装promie的执行sql语句方法
const exec = (sql, parmas) => {
    return new Promise((resolve, reject) => {
        pool.getConnection((err, conn) => {
            if (err) {
                console.log("连接数据库失败:" + err.sqlMessage);
                reject(err)
            };

            conn.query(sql, parmas, (err, result) => {
                if (err) {
                    console.log("执行sql语句失败:" + err.sqlMessage);
                    reject(err)
                }
                resolve(result);
                conn.release(); //释放连接
            })
        })
    })
}
module.exports = { exec }; //暴露方法